Skip to contentMethod: lambda$2(OWLClassT, OWLClassT)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.query.runner;
16:
17: import cz.cvut.kbss.jopa.model.query.TypedQuery;
18: import cz.cvut.kbss.jopa.model.query.criteria.*;
19: import cz.cvut.kbss.jopa.sessions.CriteriaBuilder;
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
24: import org.junit.jupiter.api.Test;
25: import org.slf4j.Logger;
26:
27: import java.util.ArrayList;
28: import java.util.Comparator;
29: import java.util.List;
30:
31: import static org.hamcrest.MatcherAssert.assertThat;
32: import static org.hamcrest.Matchers.lessThanOrEqualTo;
33: import static org.junit.jupiter.api.Assertions.*;
34:
35: public abstract class CriteriaRunner extends BaseQueryRunner {
36:
37: protected CriteriaRunner(Logger logger, DataAccessor dataAccessor) {
38: super(logger, dataAccessor);
39: }
40:
41: @Test
42: public void testSimpleFindAll() {
43: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
44: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
45: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
46: Root<OWLClassA> root = query.from(OWLClassA.class);
47: query.select(root);
48: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
49: final List<OWLClassA> result = tq.getResultList();
50:
51: assertEquals(expected.size(), result.size());
52: for (OWLClassA a : result) {
53: assertNotNull(a.getStringAttribute());
54: assertTrue(expected.stream().anyMatch(aa -> aa.getUri().equals(a.getUri())));
55: }
56: }
57:
58: @Test
59: public void testSimpleCount() {
60: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
61: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
62: CriteriaQuery<Integer> query = cb.createQuery(Integer.class);
63: Root<OWLClassA> root = query.from(OWLClassA.class);
64: query.select(cb.count(root));
65: final Integer result = getEntityManager().createQuery(query).getSingleResult();
66:
67: assertEquals(expected.size(), result);
68: }
69:
70: @Test
71: public void testFindByDataPropertyAttribute() {
72: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
73: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
74: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
75: Root<OWLClassA> root = query.from(OWLClassA.class);
76: Predicate restriction = cb.equal(root.getAttr(OWLClassA_.stringAttribute),expected.getStringAttribute(),"en");
77: query.select(root).where(restriction);
78: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
79: final OWLClassA result = tq.getSingleResult();
80:
81: assertEquals(expected.getUri(), result.getUri());
82: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
83: assertEquals(expected.getTypes(), result.getTypes());
84: }
85:
86: @Test
87: public void testFindByDataNotPropertyAttribute() {
88: final OWLClassA unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
89: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
90: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
91: Root<OWLClassA> root = query.from(OWLClassA.class);
92: Predicate restriction = cb.equal(root.getAttr(OWLClassA_.stringAttribute), unexpected.getStringAttribute(),"en");
93: query.select(root).where(cb.not(restriction));
94: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
95: final List<OWLClassA> result = tq.getResultList();
96:
97: for (OWLClassA item : result) {
98: assertNotEquals(unexpected.getUri(), item.getUri());
99: assertNotEquals(unexpected.getStringAttribute(), item.getStringAttribute());
100: }
101: }
102:
103: @Test
104: public void testFindByDataNotPropertyAttributeAndPropertyAttribute() {
105: final OWLClassT unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
106: final int intThreshold = QueryTestEnvironment.getData(OWLClassT.class).size() / 2;
107: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
108: CriteriaQuery<OWLClassT> query = cb.createQuery(OWLClassT.class);
109: Root<OWLClassT> root = query.from(OWLClassT.class);
110: Predicate firstRestriction = cb.not(cb.equal(root.getAttr("owlClassA"), unexpected.getOwlClassA().getUri()));
111: Predicate secondRestriction = cb.lessThan(root.getAttr("intAttribute"), intThreshold);
112: Predicate restrictions = cb.and(firstRestriction,secondRestriction);
113: query.select(root).where(restrictions);
114: TypedQuery<OWLClassT> tq = getEntityManager().createQuery(query);
115: final List<OWLClassT> result = tq.getResultList();
116:
117: assertFalse(result.isEmpty());
118: for (OWLClassT item : result) {
119: assertNotEquals(unexpected.getUri(), item.getUri());
120: assertTrue(intThreshold > item.getIntAttribute());
121: }
122: }
123:
124: @Test
125: public void testFindByObjectPropertyAttribute() {
126: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
127: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
128: CriteriaQuery<OWLClassD> query = cb.createQuery(OWLClassD.class);
129: Root<OWLClassD> root = query.from(OWLClassD.class);
130: Predicate restriction = cb.equal(root.getAttr("owlClassA"),expected.getOwlClassA().getUri());
131: query.select(root).where(restriction);
132: TypedQuery<OWLClassD> tq = getEntityManager().createQuery(query);
133: final OWLClassD result = tq.getSingleResult();
134:
135: assertEquals(expected.getUri(), result.getUri());
136: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
137: }
138:
139: @Test
140: public void testFindByConjunctionOfAttributes() {
141: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
142: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
143: CriteriaQuery<OWLClassT> query = cb.createQuery(OWLClassT.class);
144: Root<OWLClassT> root = query.from(OWLClassT.class);
145: Predicate firstRestriction = cb.equal(root.getAttr("owlClassA"),sample.getOwlClassA().getUri());
146: Predicate secondRestriction = cb.lessThanOrEqual(root.getAttr("intAttribute"),sample.getIntAttribute());
147: query.select(root).where(firstRestriction,secondRestriction);
148: TypedQuery<OWLClassT> tq = getEntityManager().createQuery(query);
149: final List<OWLClassT> result = tq.getResultList();
150:
151: assertFalse(result.isEmpty());
152: for (OWLClassT item : result) {
153: assertEquals(sample.getOwlClassA().getUri(), item.getOwlClassA().getUri());
154: assertThat(item.getIntAttribute(), lessThanOrEqualTo(sample.getIntAttribute()));
155: }
156: }
157:
158: @Test
159: public void testFindByConjunctionOfAttributesInList() {
160: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
161: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
162: CriteriaQuery<OWLClassT> query = cb.createQuery(OWLClassT.class);
163: Root<OWLClassT> root = query.from(OWLClassT.class);
164: List<Predicate> restrictions = new ArrayList<>();
165: restrictions.add(cb.equal(root.getAttr("owlClassA"),sample.getOwlClassA().getUri()));
166: restrictions.add(cb.lessThanOrEqual(root.getAttr("intAttribute"),sample.getIntAttribute()));
167: query.select(root).where(restrictions);
168: TypedQuery<OWLClassT> tq = getEntityManager().createQuery(query);
169: final List<OWLClassT> result = tq.getResultList();
170:
171: assertFalse(result.isEmpty());
172: for (OWLClassT item : result) {
173: assertEquals(sample.getOwlClassA().getUri(), item.getOwlClassA().getUri());
174: assertThat(item.getIntAttribute(), lessThanOrEqualTo(sample.getIntAttribute()));
175: }
176: }
177:
178: @Test
179: public void testOrderBy() {
180: final List<OWLClassT> expected = QueryTestEnvironment.getData(OWLClassT.class);
181: expected.sort(Comparator.comparing(OWLClassT::getIntAttribute));
182: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
183: CriteriaQuery<OWLClassT> query = cb.createQuery(OWLClassT.class);
184: Root<OWLClassT> root = query.from(OWLClassT.class);
185: query.select(root).orderBy(cb.asc(root.getAttr("intAttribute")));
186: TypedQuery<OWLClassT> tq = getEntityManager().createQuery(query);
187: final List<OWLClassT> result = tq.getResultList();
188:
189: assertEquals(expected.size(), result.size());
190: for (OWLClassT t : result) {
191: assertTrue(expected.stream().anyMatch(tt -> tt.getUri().equals(t.getUri())));
192: }
193: }
194:
195: @Test
196: public void testFindByDisjunctionOfAttributes() {
197: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
198: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
199: CriteriaQuery<OWLClassT> query = cb.createQuery(OWLClassT.class);
200: Root<OWLClassT> root = query.from(OWLClassT.class);
201: Predicate firstRestriction = cb.equal(root.getAttr("owlClassA"),sample.getOwlClassA().getUri());
202: Predicate secondRestriction = cb.lessThanOrEqual(root.getAttr("intAttribute"),sample.getIntAttribute());
203: query.select(root).where(cb.or(firstRestriction,secondRestriction));
204: TypedQuery<OWLClassT> tq = getEntityManager().createQuery(query);
205: final List<OWLClassT> result = tq.getResultList();
206:
207: assertFalse(result.isEmpty());
208: for (OWLClassT item : result) {
209: boolean matches = item.getOwlClassA().getUri().equals(sample.getOwlClassA().getUri());
210: matches |= item.getIntAttribute() <= sample.getIntAttribute();
211: assertTrue(matches);
212: }
213: }
214:
215: @Test
216: public void testFindByTransitiveAttributeValue() {
217: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
218: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
219: CriteriaQuery<OWLClassD> query = cb.createQuery(OWLClassD.class);
220: Root<OWLClassD> root = query.from(OWLClassD.class);
221: Predicate restrictions = cb.equal(root.getAttr("owlClassA").getAttr("stringAttribute"), expected.getOwlClassA().getStringAttribute(),"en");
222: query.select(root).where(restrictions);
223: TypedQuery<OWLClassD> tq = getEntityManager().createQuery(query);
224: final OWLClassD result = tq.getSingleResult();
225:
226: assertEquals(expected.getUri(), result.getUri());
227: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
228: }
229:
230: @Test
231: public void testFindByParameterExpression() {
232: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
233: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
234: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
235: Root<OWLClassA> root = query.from(OWLClassA.class);
236: final ParameterExpression<String> strAtt = cb.parameter(String.class, "pOne");
237: Predicate restriction = cb.equal(root.getAttr(OWLClassA_.stringAttribute), strAtt);
238: query.select(root).where(restriction);
239: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
240: tq.setParameter(strAtt, expected.getStringAttribute(), "en");
241: final OWLClassA result = tq.getSingleResult();
242:
243: assertEquals(expected.getUri(), result.getUri());
244: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
245: assertEquals(expected.getTypes(), result.getTypes());
246: }
247:
248: @Test
249: public void testFindByUnnamedParameterExpression() {
250: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
251: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
252: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
253: Root<OWLClassA> root = query.from(OWLClassA.class);
254: final ParameterExpression<String> strAtt = cb.parameter(String.class);
255: Predicate restriction = cb.equal(root.getAttr(OWLClassA_.stringAttribute), strAtt);
256: query.select(root).where(restriction);
257: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
258: tq.setParameter(strAtt, expected.getStringAttribute(), "en");
259: final OWLClassA result = tq.getSingleResult();
260:
261: assertEquals(expected.getUri(), result.getUri());
262: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
263: assertEquals(expected.getTypes(), result.getTypes());
264: }
265:
266: @Test
267: public void testFindByLiteral() {
268: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
269: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
270: CriteriaQuery<OWLClassA> query = cb.createQuery(OWLClassA.class);
271: Root<OWLClassA> root = query.from(OWLClassA.class);
272: Predicate restriction = cb.equal(root.getAttr(OWLClassA_.stringAttribute), cb.literal(expected.getStringAttribute(),"en"));
273: query.select(root).where(restriction);
274: TypedQuery<OWLClassA> tq = getEntityManager().createQuery(query);
275: final OWLClassA result = tq.getSingleResult();
276:
277: assertEquals(expected.getUri(), result.getUri());
278: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
279: assertEquals(expected.getTypes(), result.getTypes());
280: }
281: }